/**
 * Created by apple on 16/3/17.
 */

import template from './roleCreate.view.html'
import viewController from './roleCreate.controller'

class Controller {
    constructor (systemService, uiNotification, $uiModal, $stateParams, $location) {
        "ngInject"
        this.uiModal  = $uiModal;
        this.notify   = uiNotification;
        this.location = $location;
        this.service  = systemService;
        this.page     = systemService.page;
        this.type     = { 0: "编码", 1: "名称" };
        this.data     = { q: "", k: "0", txt: "按" + this.type[ 0 ] + "搜索"};

        angular.extend(this.page, {
            currentPage: $stateParams.page ? $stateParams.page : 1
        });

        this.getRole()
    }

    getRole (force) {
        // 阻止重复请求
        if (this.isLoading) return;
        this.isLoading = true;
        // 请求数据
        this.service.getList('role', this.page).then((res) => {
            this.isLoading       = false;
            this.dataList        = res.data.list;
            this.records         = res.data.list;
            this.recordsTotal    = res.data.total;
            this.page.totalItems = res.data.total;
            // 空数据提示
            this.empty = res.data.total ? false : true;
            // 是否分页
            this.hasPager = ( res.data.total > this.page.perPage ) ? true : false;

            if (force) {
                this.dLoading = false;
            }
        })
    }

    toggleConfirm (item) {
        item.confirm = !item.confirm;
    }

    del (id) {
        if (this.dLoading) return;
        this.dLoading = true;
        this.service.delItem('role', {
            sysRoleId: id
        }).then(() => {
            this.getRole(1)
        }).catch((data) => {
            this.notify.error({
                'title'  : '提示',
                'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
            });
        })
    }

    changePage (currentPage) {
        this.location.search('page', currentPage);
        this.service.getList('role', this.page).then((res) => {
            this.dataList = res.data.list;
        })
    }

    changeType () {
        this.data.txt = "按" + this.type[ this.data.k ] + "搜索"
    }

    toSearch (param) {
        this.service.searchRole(param).then((res) => {
            if ( res.data.total < this.page.perPage ) {
                this.hasPager = false;
            }
            if ( res.data.total == 0 ) {
                this.noData   = true;
            }
            this.dataList = res.data.list;
        }).finally(() => {
            this.searching = false;
        })
    }

    makeSearch ($event) {
        if ( this.searching ) return;

        if ( this.data.q == "" ) {
            this.dataList  = this.records;
            this.searching = false;
            this.noData    = false;
            if (this.recordsTotal >  this.page.perPage) this.hasPager = true;
            return;
        }

        let param      = {
            [this.data.k == 0 ? 'sysRoleCode' : 'sysRoleName'] : this.data.q
        };

        if ( $event ) {
            if ( $event.keyCode !== 13) return;
            return this.toSearch(param);
        }
        this.toSearch(param);
    }

    openCreate (item, size) {
        let modalInstance = this.uiModal.open({
            animation   : true,
            templateUrl : template,
            controller  : viewController,
            controllerAs: 'vm',
            size        : 'md' || size,
            resolve     : {
                item: () => {
                    return item
                },
                plat: () => {
                    return this.service.getPlat()
                }
            }
        });

        // 处理对话框提交/关闭
        modalInstance.result.then(() => {
            this.getRole(1)
        });
    }
}

export default Controller;

